package com.naiterui.ehp.bs.doctor.service.impl;

import com.naiterui.ehp.bp.constants.CommonConstant.ConsultType;
import com.naiterui.ehp.bp.domain.Doctor;
import com.naiterui.ehp.bp.domain.DrChargeConfig;
import com.naiterui.ehp.bp.domain.charge.DrServiceItems;
import com.naiterui.ehp.bs.doctor.dao.IDrServiceItemsDao;
import com.naiterui.ehp.bs.doctor.prodocl.resverse.VideoBookingSettingsService;
import com.naiterui.ehp.bs.doctor.service.IDoctorChargeService;
import com.naiterui.ehp.bs.doctor.service.IDrSerItemsService;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;


/**
 * 医生服务项目业务实现
 *
 * @author Amber
 * @since 1.0.0
 */
@Service
public class AdDrSerItemsServiceImpl implements IDrSerItemsService {
    private static final Logger LOGGER = LoggerFactory.getLogger(AdDrSerItemsServiceImpl.class);
    @Autowired
    private IDrServiceItemsDao drServiceItemsDao;
    @Autowired
    private IDoctorChargeService doctorChargeService;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void saveInitServiceItems(Doctor doctor) {
        try {
            // 默认开启图文咨询服务，收费0
            updateServiceItems(doctor.getId(), 1, DrChargeConfig.TYPE_MESSAGE);
            // 初始化收费信息
            doctorChargeService.saveDefaultCharge(doctor, DrChargeConfig.TYPE_MESSAGE);
        } catch (Exception e) {
            LOGGER.error("初始化服务信息失败，doctorId {}", doctor == null ? null : doctor.getId(), e);
        }

    }


    /**
     * 更新医生已开启服务信息
     * IDrSerItemsService.updateServiceItems()
     *
     * @param doctorId
     * @param open
     * @param type
     *
     * @return
     *
     * @Author Amber
     * @Date 2016年8月2日
     * @since 1.0.0
     */
    private DrServiceItems updateServiceItems(Long doctorId, Integer open, int type) {

        // 获取医生收费项目
        DrServiceItems drServiceItems = drServiceItemsDao.getDoctorChargeItems(doctorId);

        if (drServiceItems == null) {
            // 临时策略，没有服务项目数据的医生，默认开启图文咨询
            drServiceItems = new DrServiceItems(doctorId, ConsultType.GRAPHIC);

        }

        // 关闭已开启的服务
        if (open == 0) {
            if (!drServiceItems.validateOpenItem(type)) {
                // TODO:关闭未开启的服务异常提示
                LOGGER.debug("关闭未开启的服务doctorId:{}， type:{}", doctorId, type);
            }
            // 关闭某项收费
            int services = VideoBookingSettingsService.markBit(drServiceItems.getServices(), (int) Math.sqrt(type), false);
            drServiceItems.setServices(services);

        } else {

            // *******************以下为开启服务或收费信息设置处理***************

            if (!drServiceItems.validateOpenItem(type)) {
                // 开启某项服务
                int services = VideoBookingSettingsService.markBit(drServiceItems.getServices(), (int) Math.sqrt(type), true);
                drServiceItems.setServices(services);
            }
        }

        // 更新医生开通服务类型信息
        drServiceItems.setChangedAt(new Date());
        if (drServiceItems.getFetchStatus() != DrServiceItems.FETCH_STATUS_UNPROCESSED) {
            drServiceItems.setFetchStatus(DrServiceItems.FETCH_STATUS_UNPROCESSED);
        }
        // 修改医生收费项目 drChargeItems
        drServiceItemsDao.saveOrUpdate(drServiceItems);
        return drServiceItems;
    }


}
